package com.ttnet.dao.implemantations;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ttnet.dao.UserDao;
import com.ttnet.dao.base.implemantations.GenericDaoImpl;
import com.ttnet.model.User;
import com.ttnet.model.enumerations.Role;

@Component
@Scope("prototype")
public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao {

	@Override
	public User getUserByUserName(String userName) {
		Criteria createCriteria = sessionFactory.getCurrentSession().createCriteria(User.class);
		createCriteria.add(Restrictions.eq("username", userName));
		try {
			return (User) createCriteria.uniqueResult();
		} catch (HibernateException e) {
			return null;
		}
	}

	@Override
	public List<User> findUserByRole(Role role) {
		Criteria createCriteria = sessionFactory.getCurrentSession().createCriteria(User.class);
		createCriteria.add(Restrictions.eq("role", role));
		try {
			return (List<User>) createCriteria.list();
		} catch (HibernateException e) {
			return new ArrayList<User>();
		}
	}

	@Override
	public List<User> findAllMailUser() {
		Criteria createCriteria = sessionFactory.getCurrentSession().createCriteria(User.class);
		createCriteria.add(Restrictions.eq("mailUser", true));
		return createCriteria.list();
	}
}
